package com.wyw.function.learning.common;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title   生成apple数据集合的测试类
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class AppleClient {

    /**
     * 生成苹果数据集合
     */
    public static List<Apple> getApples () {
        Apple apple1 = new Apple("red", 180);
        Apple apple2 = new Apple("red", 150);
        Apple apple3 = new Apple("green", 160);

        List<Apple> apples = new ArrayList<>();
        apples.add(apple1);
        apples.add(apple2);
        apples.add(apple3);

        return apples;
    }

    /**
     * 过滤苹果
     */
    public static List<Apple> fileterApples(List<Apple> apples, ApplePredicate applePredicate) {
        List<Apple> arrayList = new ArrayList<>();

        for (Apple apple: apples) {
            if (applePredicate.test(apple)) {
                arrayList.add(apple);
            }
        }
        return arrayList;
    }

    /**
     * 过滤苹果（使用泛型）
     */
    public static <T> List<T> fileterApplesT(List<T> list,  CustomPredicate predicate) {
        List<T> arrayList = new ArrayList<>();

        for(T e: list) {
            if(predicate.test(e)) {
                arrayList.add(e);
            }
        }

        return arrayList;
    }

    /**
     * 循环打印苹果属性
     */
    public static void prettyPrintApple(List<Apple> apples, AppleFormat appleFormat) {
        for (Apple apple : apples) {
            String output = appleFormat.accept(apple);
            System.out.println(output);
        }
    }

}
